<?php

class User_Dao{
	
	function __construct(){
			$koneksi = new Koneksi();
			$koneksi->_connect();
	}
	
	function add(User $user){
		
		$sql = "
			INSERT 
			INTO
			user
			(
				email,
				password,
				level
			)
			VALUES
			(
				'".$user->email."',
				'".$user->password."',
				'".$user->level."'
			)
		";
		mysql_query($sql);
	
	}
	
	function get($id){
		
		$sql = "
		SELECT
		*
		FROM
		user
		WHERE
		id = '".$id."'
		";
		
		$user = false;
		
		$res = mysql_query($sql);
		
		if($res){
		
			while ($row = mysql_fetch_assoc($res)){
		
			$user = new User();
			$user->id = $row['id'];
			$user->email = $row['email'];
			$user->password = $row['password'];
			$user->level = $row['level'];
		}
		}
		return $user;
	}
	function get_toko($id){
	
		
		$sql = "
		SELECT
		*
		FROM
		user
		WHERE
		id = '".$id."'
		";
		
		$list_user = array();
		
		$res = mysql_query($sql);
		
		if($res){
		
			while($row = mysql_fetch_assoc($res)){
		
				$user = new User();
				
				$user->id = $row['id'];
				$user->email = $row['email'];
				$user->password = $row['password'];
				$user->level = $row['level'];

				$list_user[] = $user;
		}
		
		return $list_user;
	}
	}
	function get_all(){
	
		
		$sql = "
		SELECT
		*
		FROM
		user
		";
		
		$list_user = array();
		
		$res = mysql_query($sql);
		if($res){
			while($row = mysql_fetch_assoc($res)){
			
				$user = new User();
				
				$user->id = $row['id'];
				$user->email = $row['email'];
				$user->password = $row['password'];
				$user->level = $row['level'];
				
				$list_user[] = $user;
			
			}
		}
		
		
		return $list_user;
	
	}
	function get_password(){
	
		
		$sql = "
		SELECT
		*
		FROM
		user
		WHERE
		id='1'
		";
		
		$list_user = array();
		
		$res = mysql_query($sql);
		if($res){
			while($row = mysql_fetch_assoc($res)){
			
				$user = new User();
				
				$user->id = $row['id'];
				$user->email = $row['email'];
				$user->password = $row['password'];
				$user->level = $row['level'];
				$list_user[] = $user;		
			}
		}
		
		return $list_user;
	
	}
	
	function edit(User $user){
		$sql ="
			UPDATE 
			user
			SET
			password='$user->password'
			WHERE 
			id='$user->id'
		";
		mysql_query($sql);
	
	}
	
	function hapus ($id){
	
		
		$sql = "
		DELETE
		FROM
		user
		WHERE
		id = '".$id."'
		";
		
		mysql_query($sql);
	
	}
	//mengecek input email dan password dan mengembalikan dalam bentuk boolean
	function cek_user(User $user){
		$sql="select count(*) as jml from user where email='$user->email' and password='$user->password'";
		mysql_query($sql);
		$data=mysql_fetch_assoc(mysql_query($sql));
		$cek=false;
		//echo $data['jml'];
		if ($data['jml']=="1"){
			$cek=true;
		}
		return $cek;
	}
	
	//mengecek input unique email dalam bentuk boolean
	function cek_email($email){
		$sql="select count(*) as jml from user where email='$email'";
		mysql_query($sql);
		$data=mysql_fetch_assoc(mysql_query($sql));
		$cek=false;
		//echo $data['jml'];
		if ($data['jml']=="0"){
			$cek=true;
		}
		return $cek;
	}
	
	//mendapatkan data user berupa array berdasarkan parameter email
	function get_email($email){
		$sql="
		select *
		from
		user
		WHERE
		email = '".$email."'
		";
		
		$user=false;		
		$data = mysql_query($sql);
		if($data){
			while($row = mysql_fetch_assoc($data)){
				$user = new User();
				
				$user->id = $row['id'];
				$user->email = $row['email'];
				$user->password = $row['password'];
				$user->level = $row['level'];
			}
		}	
		return $user;
	}
	
	function randomPass(){

	$digit = 18;
	$karakter = "abcdefghijklmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ123456789";
	$i = 0;
	$pass = "";
	while ($i <= $digit-1){
		$num = rand() % 61;
		$tmp = substr($karakter,$num,1);
		$pass = $pass.$tmp;
		$i++;
	}
	return $pass;
	}

}

class User{

	var $id;
	var $email;
	var $password;
	var $level;

}